package com.jkd.server.timer;

import cn.hutool.core.convert.Convert;
import com.jkd.server.cache.ChannelMap;
import com.jkd.utils.RedisUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 定时发送读取指令定时器 当前因为协议不标准的问题导致很多问题无法处理。没有使用
 */
@Component
public class HashedWheelReader {

    @Resource
    private RedisUtil redisUtil;


    /**
     * 定时默认时间，单位分钟
     */
    private static final long DEFAULT_TICK_DURATION = 5L;
    private final Timer timer = new HashedWheelTimer();

    /**
     * 每5分钟执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer5Min(String remoteAddress, String instruction) {
        this.executeScheduleTask(DEFAULT_TICK_DURATION, TimeUnit.MINUTES, remoteAddress, instruction);
    }

    /**
     * 每15分钟执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer15Min(String remoteAddress, String instruction) {
        this.executeScheduleTask(15, TimeUnit.MINUTES, remoteAddress, instruction);
    }

    /**
     * 每30分钟执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer30Min(String remoteAddress, String instruction) {
        this.executeScheduleTask(30, TimeUnit.MINUTES, remoteAddress, instruction);
    }

    /**
     * 每1小时执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer1Hour(String remoteAddress, String instruction) {
        this.executeScheduleTask(1, TimeUnit.HOURS, remoteAddress, instruction);
    }

    /**
     * 每12小时执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer12Hour(String remoteAddress, String instruction) {
        this.executeScheduleTask(12, TimeUnit.HOURS, remoteAddress, instruction);
    }

    /**
     * 每24小时执行一次
     *
     * @param remoteAddress 通道IP
     * @param instruction  指令
     */
    public void executePer24Hour(String remoteAddress, String instruction) {
        this.executeScheduleTask(24, TimeUnit.HOURS, remoteAddress, instruction);
    }

    /**
     * 执行定时任务
     */
    private void executeScheduleTask(long time, TimeUnit timeUnit, String remoteAddress, String instruction) {
        timer.newTimeout(timeout -> {
            String[] split = instruction.split(",");
            ChannelHandlerContext ctx = ChannelMap.getChannelFromMap(remoteAddress);
            for (String s : split) {
                ctx.writeAndFlush(s);
                if (split.length > 1) {
                    redisUtil.set(ctx.channel().remoteAddress().toString() + "sensorNum", getNumber(s));
                    Thread.sleep(10000);
                }
            }
        }, time, timeUnit);
    }

    //获取指令中的传感器编号
    public String getNumber(String str) {
        String hex = Convert.hexToStr(str, Charset.forName("UTF-8"));
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(hex);
        String result = m.replaceAll("").trim();
        return result;
    }

}
